<template>
  <div>
    <el-menu :default-active="$route.path">
      <!-- 显示有多个子路由或设置了 alwaysShow 的父级路由 -->
      <el-submenu v-for="(route, index) in filteredRoutes" :key="index" v-if="shouldShowParent(route)">
        <template slot="title">{{ route.name }}</template>
        <el-menu-item v-for="child in route.children" :key="child.path" :index="child.path">
          <router-link :to="child.path">{{ child.name }}</router-link>
        </el-menu-item>
      </el-submenu>
      <!-- 显示只有一个子路由且没有设置 alwaysShow 的子路由 -->
      <el-menu-item v-else-if="route.children.length === 1" :index="route.children[0].path">
        <router-link :to="route.children[0].path">{{ route.children[0].name }}</router-link>
      </el-menu-item>
    </el-menu>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  computed: {
    filteredRoutes() {
      // 过滤出含有子路由的父级路由
      return this.$router.options.routes.filter(route => route.children);
    },
    shouldShowParent() {
      return route => route.children && (route.children.length > 1 || route.alwaysShow);
    }
  }
};
</script>


<style scoped>
.t1 {
  display: flex;
  justify-content: center;
  align-items: center;
}
.menu {
  background-color: rgb(88, 103, 131);
  .el-aside {
    height: 100%;
    .el-menu {
      height: 100%;
    }
  }
}
.logo {
  height: 50px;
  width: 50px;
  vertical-align: middle;
  border-radius: 50%;
}
.title {
  font-size: 20px;
  color: aliceblue;
}
</style>>